package cn.nawang.ebeim.client.version;

/**
 * Created by GanJc on 2016/2/22.
 */
public class Back {

    public static void main(String[] args) {
        longestPalindrome("abab");//3
        longestPalindrome("bbbb");//4
        longestPalindrome("abba");//4
    }

    public static void longestPalindrome(String s) {
        s = s.replaceAll("", "#");
        String longest = "";
        for (int i = 0; i < s.length() - 1; i++) {
            String p = expandAroundCenter(s, i);
            if (p.length() > longest.length()) {
                longest = p;
            }
        }
        System.out.println("longest:" + longest.replace("#", "") +",length:" + longest.length() / 2);
    }

    public static String expandAroundCenter(String s, int index) {
        String str = s.substring(index, index + 1);
        int l = index, r = index;
        for (; ; l--, r++) {
            if (l >= 0 && r < s.length() && s.charAt(l) == s.charAt(r)) {
                str = s.substring(l, r + 1);
            } else {
                break;
            }
        }
        return str;
    }

}
